package cc.togetherit.jcrawler.parse;

import java.util.Map;

import cc.togetherit.jcrawler.dataPool.CrawlerPool;
import cc.togetherit.jcrawler.fetch.FetchRequest;

public abstract class AbstractParseEngine implements ParseEngine, Runnable {
    private CrawlerPool<FetchRequest> parsePool;
    private CrawlerPool<Map<String, Object>> persistencePool;
    private boolean isstop = false;
    private ParseConfig parseConfig;
    private boolean isParseEngine = true;



    public void run() {
        if (isParseEngine) {
            startParse();
        } else {
            startPersitence();
        }

    }

    public AbstractParseEngine(CrawlerPool<FetchRequest> parsePool, CrawlerPool<Map<String, Object>> persistencePool, ParseConfig parseConfig, boolean isParseEngine) {
        super();
        this.parsePool = parsePool;
        this.persistencePool = persistencePool;
        this.parseConfig = parseConfig;
        this.isParseEngine = isParseEngine;
    }

    public CrawlerPool<FetchRequest> getParsePool() {
        return parsePool;
    }

    public void setParsePool(CrawlerPool<FetchRequest> parsePool) {
        this.parsePool = parsePool;
    }

    public CrawlerPool<Map<String, Object>> getPersistencePool() {
        return persistencePool;
    }

    public void setPersistencePool(CrawlerPool<Map<String, Object>> persistencePool) {
        this.persistencePool = persistencePool;
    }

    public boolean isIsstop() {
        return isstop;
    }

    public void setIsstop(boolean isstop) {
        this.isstop = isstop;
    }


    public ParseConfig getParseConfig() {
        return parseConfig;
    }

    public void setParseConfig(ParseConfig parseConfig) {
        this.parseConfig = parseConfig;
    }

    public void startParse() {
        while (!isstop) {
            FetchRequest f = parsePool.consume();
            if (f == null) {
                f = parsePool.consume();
                if (f == null) {
                    isstop = true;
                    continue;
                }
                Map<String, Object> m = parse(f.getContent());
                persistencePool.produce(m);
            }
        }
    }


    public void startPersitence() {
        while (!isstop) {
            Map<String, Object> f = persistencePool.consume();
            if (f == null) {
                f = persistencePool.consume();
                if (f == null) {
                    isstop = true;
                }
                persistence(f);
            }
        }
    }



    public boolean isParseEngine() {
        return isParseEngine;
    }

    public void setParseEngine(boolean isParseEngine) {
        this.isParseEngine = isParseEngine;
    }

    public abstract Map<String, Object> parse(String content);

    public abstract boolean persistence(Map<String, Object> metaInfo);

}
